package com.hadoop.demo;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * =================================
 * <p>
 * Created by cjj on 18-9-29.
 * <p>
 * 描述: hadoop 文件上传
 */

public class HdfsUtil {

    public static void main(String[] args) throws IOException {

    }

    /**
     * 下载文件到本地
     */
    @Test
    public  void downloadHadoop() throws IOException {

        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(conf);

        FSDataInputStream is = fileSystem.open(new Path("hdfs://cj.sun.hadoop:9000/README.html"));
        FileOutputStream os = new FileOutputStream("/home/cjj/project/docker-common/hadoopdemo/init.html");
        IOUtils.copy(is,os);
    }

    /**
     * 上传文件到hadoop
     */
    @Test
    public  void uploadHadoop() throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://cj.sun.hadoop:9000/");
        FileSystem fileSystem = FileSystem.get(conf);

        FSDataOutputStream os = fileSystem.create(new Path("hdfs://cj.sun.hadoop:9000/hadoop.pdf"));
        FileInputStream is = new FileInputStream("/home/cjj/project/docker-common/hadoopdemo/hadoop-shell.pdf");
        IOUtils.copy(is,os);
    }

    //简便的方式上传与下载

}
